﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.WebModel;
using Interactive.Utility.Extension;
using TravelLink.ScheduleTasks.Service;
namespace TravelLink.WebApp.UserControl.Member {
    public partial class UC_Member_Index : ControlBase {


        public string CityNames { get; set; }
        public string CityValues { get; set; }


        public string IndustryNames { get; set; }
        public string IndustryValues { get; set; }


        protected void Page_Load(object sender, EventArgs e) {



            if (!IsPostBack) {
                this.drpPositionLv2.DataSource = this.DB.TL_PositionLevel;
                this.drpPositionLv2.DataTextField = "Name";
                this.drpPositionLv2.DataValueField = "Idx";
                this.drpPositionLv2.DataBind();
             //   this.drpPositionLv2.Items.Insert(0, new ListItem("--不限--", "-1"));



                this.drpPositionJobType2.DataSource = this.DB.TL_PositionType;
                this.drpPositionJobType2.DataTextField = "Name";
                this.drpPositionJobType2.DataValueField = "Idx";
                this.drpPositionJobType2.DataBind();
              //  this.drpPositionJobType2.Items.Insert(0, new ListItem("--不限--", "-1"));
              
                BindSub();
            }
        }


        private void BindSub() {
            var subscriptionEntity = this.DB.TL_User_Subscription.Where(u => u.UserCode == this.CurrentUser.DBUser.UserCode).FirstOrDefault();
            if (subscriptionEntity != null) {
                if (subscriptionEntity.Period.HasValue) {
                    this.dropPublish.SelectedValue = subscriptionEntity.Period.Value.ToString();
                }
                if (!string.IsNullOrEmpty(subscriptionEntity.PositionLevel)) {
                    string[] ar = subscriptionEntity.PositionLevel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < drpPositionLv2.Items.Count; i++) {
                        if (ar.Contains(drpPositionLv2.Items[i].Value)) {
                            drpPositionLv2.Items[i].Selected = true;
                        }
                    }
                    
                }
                if (!string.IsNullOrEmpty(subscriptionEntity.PositionType)) {
                    string[] ar = subscriptionEntity.PositionType.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < drpPositionJobType2.Items.Count; i++) {
                        if (ar.Contains(drpPositionJobType2.Items[i].Value)) {
                            drpPositionJobType2.Items[i].Selected = true;
                        }
                    }

                   // drpPositionJobType2.SelectedValue = subscriptionEntity.PositionType;
                }
                if (!string.IsNullOrEmpty(subscriptionEntity.City)) {
                    List<int> values = subscriptionEntity.City.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(u => u.AsInt()).ToList();

                    CityValues = subscriptionEntity.City;
                    if (values.Count > 0 && values.IndexOf(-1) < 0) {
                        CityNames = string.Join(",", this.DB.TL_City.Where(u => values.Contains(u.Idx)).Select(u => u.CName).ToList());
                    }
                    else {
                        CityNames = "不限";
                    }
                }
                else {
                    CityValues = string.Empty;
                    CityNames = string.Empty;
                }
                if (!string.IsNullOrEmpty(subscriptionEntity.IndustryType)) {

                    List<int> values = subscriptionEntity.IndustryType.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(u => u.AsInt()).ToList();

                    IndustryValues = subscriptionEntity.IndustryType;
                    if (values.Count > 0 && values.IndexOf(-1) < 0) {
                        IndustryNames = string.Join(",", this.DB.TL_IndustryType2.Where(u => values.Contains(u.Idx)).Select(u => u.Name).ToList());
                    }
                    else {
                        IndustryNames = "不限";
                    }
                }
                else {
                    IndustryValues = string.Empty;
                    IndustryNames = string.Empty;
                }
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e) {
            string pub = this.dropPublish.SelectedValue.ToString();
            string indutrys = Request.Form["industrytype_val"];
            string citys = Request.Form["city_val"];

            string level = "";
            for (int i = 0; i < drpPositionLv2.Items.Count; i++) {
                if (drpPositionLv2.Items[i].Selected)
                    level += drpPositionLv2.Items[i].Value + ",";
            }

            string type = "";
            for (int i = 0; i < drpPositionJobType2.Items.Count; i++) {
                if (drpPositionJobType2.Items[i].Selected)
                    type += drpPositionJobType2.Items[i].Value + ",";
            }

            bool isAdd = false;
            var subscriptionEntity = this.DB.TL_User_Subscription.Where(u => u.UserCode == this.CurrentUser.DBUser.UserCode).FirstOrDefault();
            if (subscriptionEntity == null) {
                subscriptionEntity = new DAL.TL_User_Subscription();

                isAdd = true;
            }
            subscriptionEntity.UserCode = this.CurrentUser.DBUser.UserCode;
            subscriptionEntity.ActionDate = DateTime.Now;
            subscriptionEntity.City = citys;
            subscriptionEntity.IndustryType = indutrys;
            subscriptionEntity.Period = pub.AsInt();
            subscriptionEntity.PositionLevel = level;
            subscriptionEntity.PositionType = type;
            subscriptionEntity.Status = 0;
            if (isAdd)
                this.DB.TL_User_Subscription.InsertOnSubmit(subscriptionEntity);

            this.DB.SubmitChanges();

            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('订阅信息保存成功!');", true);


            new MailSvc().SendPostionInfo(subscriptionEntity.UserCode);


            BindSub();
        }
    }
}